/**
 * Electron 服务层
 * 处理渲染进程与主进程之间的通信
 */

// 渲染进程与主进程通信接口
interface ElectronAPI {
  ipcRenderer: {
    send: (channel: string, ...args: any[]) => void;
    invoke: (channel: string, ...args: any[]) => Promise<any>;
    on: (channel: string, listener: (...args: any[]) => void) => void;
    removeListener: (channel: string, listener: (...args: any[]) => void) => void;
  };
}

// 获取 Electron API
const electronAPI = (window as any).electron as ElectronAPI;

/**
 * Electron 服务
 */
export const ElectronService = {
  /**
   * 用户认证相关
   */
  async login(username: string, password: string) {
    return electronAPI.ipcRenderer.invoke('login', { username, password });
  },

  async register(userData: { username: string; email: string; password: string }) {
    return electronAPI.ipcRenderer.invoke('register', { userData });
  },

  async getUserProfile() {
    return electronAPI.ipcRenderer.invoke('get-user-profile');
  },

  async getUserSettings() {
    return electronAPI.ipcRenderer.invoke('get-user-settings');
  },

  async updateUserSettings(settings: any) {
    return electronAPI.ipcRenderer.invoke('update-user-settings', { settings });
  },

  /**
   * 项目相关
   */
  async getProjects() {
    return electronAPI.ipcRenderer.invoke('get-projects');
  },

  async getProject(id: string) {
    return electronAPI.ipcRenderer.invoke('get-project', { id });
  },

  async createProject(projectData: any) {
    return electronAPI.ipcRenderer.invoke('create-project', { project: projectData });
  },

  async updateProject(projectData: any) {
    return electronAPI.ipcRenderer.invoke('update-project', { project: projectData });
  },

  async deleteProject(id: string) {
    return electronAPI.ipcRenderer.invoke('delete-project', { id });
  },

  async saveScene(sceneData: any) {
    return electronAPI.ipcRenderer.invoke('save-scene', { scene: sceneData });
  },

  async saveCharacter(characterData: any) {
    return electronAPI.ipcRenderer.invoke('save-character', { character: characterData });
  },

  async exportProject(options: any) {
    return electronAPI.ipcRenderer.invoke('export-project', { options });
  },

  /**
   * 素材相关
   */
  async getAssetLibraries() {
    return electronAPI.ipcRenderer.invoke('get-asset-libraries');
  },

  async getAssetLibrary(id: string) {
    return electronAPI.ipcRenderer.invoke('get-asset-library', { id });
  },

  async getAssets(params: { 
    libraryId?: string;
    categoryId?: string;
    type?: string;
    search?: string;
    page?: number;
    pageSize?: number;
    sortBy?: string;
    sortOrder?: 'asc' | 'desc';
  }) {
    return electronAPI.ipcRenderer.invoke('get-assets', params);
  },

  async getAsset(id: string) {
    return electronAPI.ipcRenderer.invoke('get-asset', { id });
  },

  async createAsset(assetData: any) {
    return electronAPI.ipcRenderer.invoke('create-asset', { asset: assetData });
  },

  async updateAsset(assetData: any) {
    return electronAPI.ipcRenderer.invoke('update-asset', { asset: assetData });
  },

  async deleteAsset(id: string) {
    return electronAPI.ipcRenderer.invoke('delete-asset', { id });
  },

  async purchaseAsset(params: { assetId?: string; assetPackId?: string; paymentMethod: string }) {
    return electronAPI.ipcRenderer.invoke('purchase-asset', params);
  },

  async downloadAsset(id: string) {
    return electronAPI.ipcRenderer.invoke('download-asset', { id });
  },

  /**
   * 插件相关
   */
  async getPlugins() {
    return electronAPI.ipcRenderer.invoke('get-plugins');
  },

  async getPlugin(id: string) {
    return electronAPI.ipcRenderer.invoke('get-plugin', { id });
  },

  async installPlugin(id: string) {
    return electronAPI.ipcRenderer.invoke('install-plugin', { id });
  },

  async uninstallPlugin(id: string) {
    return electronAPI.ipcRenderer.invoke('uninstall-plugin', { id });
  },

  async enablePlugin(id: string) {
    return electronAPI.ipcRenderer.invoke('enable-plugin', { id });
  },

  async disablePlugin(id: string) {
    return electronAPI.ipcRenderer.invoke('disable-plugin', { id });
  },

  /**
   * AI 相关
   */
  async generateFromText(prompt: string, options: any) {
    return electronAPI.ipcRenderer.invoke('generate-from-text', { prompt, options });
  },

  async trainAIModel(data: any) {
    return electronAPI.ipcRenderer.invoke('train-ai-model', { data });
  },

  /**
   * 系统相关
   */
  async getSystemInfo() {
    return electronAPI.ipcRenderer.invoke('get-system-info');
  },

  async checkForUpdates() {
    return electronAPI.ipcRenderer.invoke('check-for-updates');
  },

  async downloadUpdate() {
    return electronAPI.ipcRenderer.invoke('download-update');
  },

  async installUpdate() {
    return electronAPI.ipcRenderer.invoke('install-update');
  },

  async openExternal(url: string) {
    return electronAPI.ipcRenderer.invoke('open-external', { url });
  },

  async showItemInFolder(path: string) {
    return electronAPI.ipcRenderer.invoke('show-item-in-folder', { path });
  },

  async selectDirectory() {
    return electronAPI.ipcRenderer.invoke('select-directory');
  },

  async selectFile(options: { filters?: { name: string; extensions: string[] }[]; properties?: string[] }) {
    return electronAPI.ipcRenderer.invoke('select-file', options);
  },

  /**
   * 系统设置相关
   */
  async getAppSettings() {
    return electronAPI.ipcRenderer.invoke('get-app-settings');
  },

  async updateAppSettings(settings: any) {
    return electronAPI.ipcRenderer.invoke('update-app-settings', { settings });
  },

  /**
   * 事件监听相关
   */
  onDownloadProgress(callback: (progress: { percent: number; transferred: number; total: number }) => void) {
    electronAPI.ipcRenderer.on('download-progress', callback);
    return () => electronAPI.ipcRenderer.removeListener('download-progress', callback);
  },

  onUpdateDownloaded(callback: () => void) {
    electronAPI.ipcRenderer.on('update-downloaded', callback);
    return () => electronAPI.ipcRenderer.removeListener('update-downloaded', callback);
  }
}; 